/*
 * @lc app=leetcode.cn id=238 lang=java
 *
 * [238] 除自身以外数组的乘积
 */

// @lc code=start
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int len = nums.length;

        int[] ans = new int[len];
        int[] right = new int[len];
        int[] left = new int[len];
        left[0] = nums[0];
        for(int i=1;i<len;i++){
            left[i] = left[i-1] * nums[i];
        }

        right[len-1] = nums[len-1];
        for(int i=len-2;i>=0;i--){
            right[i] = right[i+1] * nums[i];
        }
        for(int i=0;i<len;i++){
            if(i == 0 ){
                ans[i] = right[i+1];
            } else if(i == len - 1){
                ans[i] = left[i-1];
            } else {
                ans[i] = left[i-1] * right[i+1];
            }
            
        }
        return ans;
    }
}
// @lc code=end

